<?php
namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\Bankinfo;
use api\controllers\OnAuthController;
use common\helpers\ResultDataHelper;
use Yii;

class BankinfoController extends OnAuthController
{
    public $modelClass = Bankinfo::class;

    protected $optional = ['index'];

    //添加银行卡信息
    public function actionAdd(){
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(403,'Access Done!');
        }
        //todo 判断银行卡是否已经存在
        $models = $this->modelClass::find()->where(['bank_code'=>Yii::$app->request->post('bank_code')])->andWhere(['<>','status','0'])->one();
        if (!empty($models)) {
            return ResultDataHelper::api(422,'银行卡已存在');
        }
        $model = new Bankinfo();
        $model->attributes = Yii::$app->request->post();
        $model->member_id = Yii::$app->user->identity->member_id;
        if (!$model->save()) {
            return ResultDataHelper::api(422,$model->getFirstErrors());
        }
        return ResultDataHelper::api(200,'添加成功，请等待审核');
    }

    //获取用户银行卡信息列表
    public function actionList(){
        if (!Yii::$app->request->isGet){
            return ResultDataHelper::api(403,'Access Done!');
        }
        $model = Bankinfo::find()->where(['member_id'=>Yii::$app->user->identity->member_id])->andWhere(['=','status','2'])->all();
        if (empty($model)) {
            return ResultDataHelper::api(422,'用户暂无银行卡信息');
        }
        return ResultDataHelper::api(200,'ok',$model);
    }

    //删除银行卡信息
    public function actionDeleteBank($id){
        if (!Yii::$app->request->isGet){
            return ResultDataHelper::api(403,'Access Done!');
        }
        if (!Bankinfo::findOne($id)->delete()) {
            return ResultDataHelper::api(422,'删除失败');
        }
        return ResultDataHelper::api(200,'删除成功');
    }
}